package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.dao.WareSkuDao;
import com.atguigu.gulimall.ware.dto.SkuHasStockDto;
import com.atguigu.gulimall.ware.entity.WareSkuEntity;
import com.atguigu.gulimall.ware.feign.WareFeignService;
import com.atguigu.gulimall.ware.service.WareSkuService;
import com.atguigu.to.SkuHasStockTo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    private WareFeignService wareFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
            QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        String wareId = (String)params.get("wareId");
        if (StringUtils.isNotBlank(wareId)) {
            wrapper.eq("ware_id", wareId);
        }
        String skuId = (String)params.get("skuId");
        if (StringUtils.isNotBlank(skuId)) {
            wrapper.eq("sku_id", skuId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("sku_id", skuId).eq("ware_id", wareId);
        List<WareSkuEntity> skuEntityList = this.list(wrapper);
        if (CollectionUtils.isEmpty(skuEntityList)) {
            //新增库存
            WareSkuEntity skuEntity = new WareSkuEntity();
            skuEntity.setSkuId(skuId);
            skuEntity.setWareId(wareId);
            skuEntity.setStock(skuNum);
            skuEntity.setStockLocked(0);
            //远程调用查skuName
            R info = wareFeignService.info(skuId);
            if (info.getCode() == 0) {
                Map<String, Object> skuInfo = (Map<String, Object>) info.get("skuInfo");
                skuEntity.setSkuName((String)skuInfo.get("skuName"));
            }
            this.save(skuEntity);
        } else {
            //更新库存
            this.baseMapper.addStock(skuId, wareId, skuNum);
        }
    }

    @Override
    public List<SkuHasStockTo> hasStock(List<Long> skuIds) {
        //没有库存记录的全是没库存
        //查到有库存记录的计算库存判断是否有库存

        if (CollectionUtils.isEmpty(skuIds)) {
            return Collections.EMPTY_LIST;
        }

        List<SkuHasStockDto> entities = this.baseMapper.hasStock(skuIds);
        System.out.println("entities = " + entities);

        Map<Long, Integer> skuMap = entities.stream().collect(Collectors.toMap(
                SkuHasStockDto::getSkuId, SkuHasStockDto::getStockCount, (k1, k2) -> k1
        ));

        List<SkuHasStockTo> voList = skuIds.stream().map(item -> {
            SkuHasStockTo vo = new SkuHasStockTo();
            vo.setSkuId(item);
            if (skuMap.containsKey(item)) {
                vo.setHasStock(skuMap.get(item) > 0);
            } else {
                vo.setHasStock(false);
            }
            return vo;
        }).collect(Collectors.toList());

        return voList;
    }

}